<?

$url = "http://www.tthfanfic.com/rss.php?author=".$_GET['aid'];
//$url = "./updates/tth.dat";
	$fdat =  file ( $url );

while(list($index,$record)=each($fdat)){
	$full_data = $full_data.$record;
}

$data_set = explode('<item>',$full_data);
while(list($index,$record)=each($data_set)){
	if(!stristr($record,'<WebMaster>')){
		//echo htmlspecialchars($record);
		$temp = explode('</title>',$record);
			$dataA[$index]['title'] = trim(str_replace('<title>','',$temp[0]));

		$temp = explode('<link>',$record);
		$temp = explode('</link>',$temp[1]);
		$temp = explode('-',$temp[0]);
		$temp = explode('/',$temp[1]);
			$dataA[$index]['id']=trim($temp[0]);

		$temp = explode('<description>',$record);
		$temp = explode('</description>',$temp[1]);
			$dataA[$index]['summary']=trim($temp[0]);

		$temp = explode('<pubDate>',$record);
		$temp = explode('</pubDate>',$temp[1]);
			$dataA[$index]['updated'] = date("Y-m-d", strtotime($temp[0]));					

		$temp = explode('<guid>',$record);
		$temp = explode('</guid>',$temp[1]);
		$temp = explode('-',$temp[0]);
		$temp = explode('/',$temp[2]);
			$dataA[$index]['chapters']=trim($temp[0]);
			if($dataA[$index]['chapters']==''){ $dataA[$index]['chapters'] = "1"; }

		$temp = explode('<dc:creator>',$record);
		$temp = explode('</dc:creator>',$temp[1]);
			$dataA[$index]['author']=trim($temp[0]);
/*		/$q_aid ="SELECT author_id FROM fic_authors WHERE sitekey='T' AND author_name='".$dataA[$index]['author']."'";
		//echo $q_aid;
		$rs_aid = $db->Execute($q_aid);
		if(!$rs_aid->EOF){
			$dataA[$index]['author_id'] = $rs_aid->Fields('author_id');
		}
*/
	$dataA[$index]['author_id'] = $aid;

/*echo '<pre>';
var_dump($dataA[$index]);
echo '</pre>';
echo '<hr>';*/
	}
}


/*echo '<pre>';
var_dump($dataA);
echo '</pre>';*/

echo "&nbsp;&nbsp;&nbsp;Scan Complete<br>";
//exit();


$cnt = 0; $cnt_n = 0; $cnt_u = 0; $cnt_Q = 0; $cnt_i = 0;

	while(list($key,$val)=each($dataA)){
		$sql = "INSERT INTO `fic_listing` ( `story_id` , `author_id` , `story_title` , `chapters` , `updated` , `status`,`summary`,`inserted` ) VALUES ('".$val['id']."', '".$val['author_id']."', '".str_replace("'","''",$val['title'])."', '".$val['chapters']."',  '".$val['updated']."', '?','".str_replace("'","''",$val['summary'])."', NOW())";
		$cnt = $cnt + 1;
		$rs1 = $db->Execute($sql);
		if($rs1===false){ 
			$err = $db->ErrorMsg();
			if(stristr($err,'Duplicate entry')){

				$sqlc = "SELECT * FROM fic_listing WHERE sitekey='T' and story_id='".$val['id']."'";
				$rsC = $db->Execute($sqlc);
				$upd = false;
				if($rsC->Fields('chapters')!=$val['chapters']){ $upd = true; }
				if($rsC->Fields('updated')!=$val['updated']){ $upd = true; }
				$insid = $rsC->Fields('id');
				$db->Execute("INSERT INTO fic_metaverse SET fic_id='".$insid ."',universe_id='13'");

				if($upd){
					$sqT = "chapters='".$val['chapters']."',updated='".$val['updated']."',inserted=NOW()";
					if($rsC->Fields('author_id')=='0'){
						$sqT .= ", author_id='".$val['author_id']."'";						
					}
						$sqT .= ", story_title='".str_replace("'","''",$val['title'])."'";	
					if($rsC->Fields('status')=='?' ){
						$cnt_Q = $cnt_Q + 1;
						$sqlU = "UPDATE fic_listing SET ".$sqT." WHERE sitekey='T' and story_id='".$val['id']."'";
						$db->Execute($sqlU);
					}elseif( $rsC->Fields('status')=='N'){
						$cnt_u = $cnt_u + 1;
						$sqlU = "UPDATE fic_listing SET ".$sqT." WHERE sitekey='T' and story_id='".$val['id']."'";
						$db->Execute($sqlU);
					}elseif($rsC->Fields('status')=='S'){	
						$cnt_u = $cnt_u + 1;
						$sqlU = "UPDATE fic_listing SET ".$sqT." WHERE sitekey='T' and story_id='".$val['id']."'";					
						$db->Execute($sqlU);
					}elseif($rsC->Fields('status')!='I'){	
						$cnt_u = $cnt_u + 1;
						$sqlU = "UPDATE fic_listing SET ".$sqT.",status='U' WHERE sitekey='T' and story_id='".$val['id']."'";						
						$db->Execute($sqlU);
					}elseif($rsC->Fields('status')=='I'){	
						$cnt_i++;
						$sqlU = "UPDATE fic_listing SET ".$sqT." WHERE sitekey='T' and story_id='".$val['id']."'";						
						$db->Execute($sqlU);
					}
				}
			}else{
				echo "<br><b>".$err."</b>".$sql."<br>";
			}
		}else{
			$insid = $db->Insert_ID();
			$db->Execute("INSERT INTO fic_metaverse SET fic_id='".$insid ."',universe_id='13'");
			$cnt_n = $cnt_n + 1;
			//if new story try to insert author		
			$sql2 = "INSERT INTO `fic_authors` ( `author_id` , `author_name` )VALUES ('".$val['author_id']."', '".str_replace("'","''",$val['author'])."')";
			$rs2 = $db->Execute($sql2);
		}
	}

echo '&nbsp;&nbsp;&nbsp;Update Completed<br>';
echo '&nbsp;&nbsp;&nbsp;total entries:'.$cnt.', ignored:'.$cnt_i.', new stories:'.$cnt_n.', updated new:'.$cnt_Q.', UPDATED:'.$cnt_u.'<br>';

$db->Execute("UPDATE fic_authors SET linkid=id WHERE linkid='0'");

?>
